package com.suyang.syt.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.auth.AuthMethod;
import com.suyang.syt.model.User;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.UserVisitorDTO;
import com.suyang.syt.service.interfaces.UserVisitorService;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/admin/user/visitor")
@AuthClass("login")
public class AdminUserVisitorController {
	@Autowired
	private UserVisitorService userVisitorService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(String uid, String status, Model model) {
		if (EmptyUtil.isNotEmpty("uid")) {
			model.addAttribute("uid", uid);
		}
		if (EmptyUtil.isNotEmpty("status")) {
			model.addAttribute("status", status);
		}
		return "user/visitor/list";
	}

	@AuthMethod(role = "ROLE_PXJG")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<UserVisitorDTO> list(HttpServletRequest request, String uid, int page, int rows, String sort, String order, String status) {
		try {
			String createDate1 = request.getParameter("createDate1");
			String createDate2 = request.getParameter("createDate2");
			return userVisitorService.listByEasyUI(uid, page, rows, sort, order, createDate1, createDate2, status);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	@AuthMethod(role = "ROLE_JGJS")
	public AjaxObj delete(HttpServletRequest request, String ids, String status) {
		try {
			return userVisitorService.delete(ids, status);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	@AuthMethod(role = "ROLE_JGJS")
	public AjaxObj add(HttpServletRequest request, String uid, String status) {
		try {
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("loginUser");
			if (user == null) {
				return new AjaxObj(0, "当前用户信息获取失败!");
			}
			long id = Long.parseLong(uid);
			return userVisitorService.add(user.getId(), id, request.getRemoteAddr(), status);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}
